<template>
  <div id="editUser">
      <AddAndUpdateTable @submitUserInfo="submitUserInfo" :userInfo="userInfo" />
  </div>
</template>

<script>

import {
  updateUserInfo,getUserInfoByUserDetailId
} from '@/assets/request/api.js';
import AddAndUpdateTable from './addAndUpdateTable.vue';
import tool from '@/assets/js/tool'
export default {
  name: "editUser",
  mixins: [tool],
  data() {
    return {
      userInfo: {},
      bmsUserPasswordChangeVO: {}
    }
  },
  methods: {
    submitUserInfo(userInfo) {
      console.log("更新用户")
      let passwordChangeVO = userInfo.bmsUserPasswordChangeVO
      passwordChangeVO.myPassword = this.rsaEncryptByPublicKey(userInfo.myPassword)
      
      userInfo.modifyPassword = false
      if(userInfo.newPassword) {
        userInfo.modifyPassword = true
        passwordChangeVO.newPassword = this.rsaEncryptByPublicKey(userInfo.newPassword)
        passwordChangeVO.confirmPassword = this.rsaEncryptByPublicKey(userInfo.confirmPassword)
      }

      userInfo.bmsUserPasswordChangeVO = passwordChangeVO

      updateUserInfo(userInfo).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.$router.go(-1)
      }).catch(err => {
        this.$message({
          message: err.message,
          type: 'error'
        });
      })
    },
    async getUserInfoByUserDetailId(userDetailId) {
      await getUserInfoByUserDetailId(userDetailId).then(res => {
        this.userInfo = res.data
      }).catch(err => {
        this.$message({
          message: err.message,
          type: 'error'
        });
      })
    }
  },
  created() {
    this.getUserInfoByUserDetailId(this.$route.params.id)
  },
  components: { AddAndUpdateTable }
}
</script>

<style lang="scss" scoped>
</style>